package com.maxmlr.mybatisplus.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxmlr.mybatisplus.resut.ResultBean;
import com.maxmlr.mybatisplus.resut.ResultPageBean;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;


/**
 * @author max
 * @version 1.0
 * @Package com.maxmlr.common.base
 * @ClassName BaseController
 * @Description 控制器基类
 * @Copyright Copyright (c) 2018: www.maxmlr.com.
 * @since 2018年12月15日 下午6:06:33
 */
public class BaseController<M extends BaseService<T>, T> {
    private static Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    protected M baseService;

    /**
     * @Title BaseController
     * @Description 通过id获取<T>
     * @author max
     * @since 2019-05-11
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "", notes = "通过id获取param")
    public ResultBean getById(@ApiParam(name = "id", required = true) @PathVariable(value = "id") Serializable id) {
        ResultBean bean = new ResultBean();
        T param = baseService.getById(id);
        bean.setData(param);
        return bean;
    }

    /**
     * @Title BaseController
     * @Description 获取分页列表
     * @author max
     * @since 2019-07-29
     */
    @GetMapping("/query")
    @ApiOperation(value = "/query", notes = "获取分页列表")
    public ResultPageBean query(@ApiParam(name = "Song", value = "Song 实体类") T param,
                                @ApiParam(name = "pageSize", value = "页大小", required = true, defaultValue = "10") @RequestParam("pageSize") Integer pageSize,
                                @ApiParam(name = "pageNum", value = "页数", required = true, defaultValue = "1") @RequestParam("pageNum") Integer pageNum) {
        ResultPageBean pageBean = new ResultPageBean();
        Page<T> page = new Page<>(pageNum, pageSize);
        baseService.page(page, new QueryWrapper(param));
        pageBean.setPage(page);
        return pageBean;
    }


    /**
     * @Title BaseController
     * @Description 添加<T>
     * @author max
     * @since 2019-07-29
     */
    @PostMapping
    @ApiOperation(value = "", notes = "添加Param")
    public ResultBean addParam(@ApiParam(name = "Param", value = "Param 实体类") @RequestBody T param) {
        ResultBean bean = new ResultBean();
        baseService.save(param);
        return bean;
    }

    /**
     * @Title BaseController
     * @Description 通过id更新<T>
     * @author max
     * @since 2019-07-29
     */
    @PutMapping
    @ApiOperation(value = "", notes = "通过id更新Param")
    public ResultBean updateById(@ApiParam(name = "Param", value = "Param 实体类") @RequestBody T param) {
        ResultBean bean = new ResultBean();
        baseService.updateById(param);
        return bean;
    }

}
